program Quake3Map;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  Windows,
  DIrr in '..\..\DSource\DIrr.pas',
  DIrr.IO in '..\..\DSource\DIrr.IO.pas',
  DIrr.Irr in '..\..\DSource\DIrr.Irr.pas',
  DIrr.Mem in '..\..\DSource\DIrr.Mem.pas',
  DIrr.GUI in '..\..\DSource\DIrr.GUI.pas',
  DIrr.Base in '..\..\DSource\DIrr.Base.pas',
  DIrr.Core in '..\..\DSource\DIrr.Core.pas',
  DIrr.Math in '..\..\DSource\DIrr.Math.pas',
  DIrr.Rect in '..\..\DSource\DIrr.Rect.pas',
  DIrr.Color in '..\..\DSource\DIrr.Color.pas',
  DIrr.Scene in '..\..\DSource\DIrr.Scene.pas',
  DIrr.Types in '..\..\DSource\DIrr.Types.pas',
  DIrr.Video in '..\..\DSource\DIrr.Video.pas',
  DIrr.Config in '..\..\DSource\DIrr.Config.pas',
  DIrr.Line2D in '..\..\DSource\DIrr.Line2D.pas',
  DIrr.Line3D in '..\..\DSource\DIrr.Line3D.pas',
  DIrr.Matrix4 in '..\..\DSource\DIrr.Matrix4.pas',
  DIrr.Plane3D in '..\..\DSource\DIrr.Plane3D.pas',
  DIrr.Aabbox3D in '..\..\DSource\DIrr.Aabbox3D.pas',
  DIrr.Vector2D in '..\..\DSource\DIrr.Vector2D.pas',
  DIrr.Vector3D in '..\..\DSource\DIrr.Vector3D.pas',
  DIrr.WrapType in '..\..\DSource\DIrr.WrapType.pas',
  DIrr.Dimension2d in '..\..\DSource\DIrr.Dimension2d.pas',
  DIrr.Consts in '..\..\DSource\DIrr.Consts.pas',
  DIrr.Quake3 in '..\..\DSource\DIrr.Quake3.pas',
  DIrr.Structs in '..\..\DSource\DIrr.Structs.pas',
  DIrr.Event in '..\..\DSource\DIrr.Event.pas',
  DIrr.Buffer in '..\..\DSource\DIrr.Buffer.pas',
  DIrr.Texture in '..\..\DSource\DIrr.Texture.pas',
  DIrr.Triangle3D in '..\..\DSource\DIrr.Triangle3D.pas',
  DIrr.Material in '..\..\DSource\DIrr.Material.pas',
  DIrr.GPU in '..\..\DSource\DIrr.GPU.pas',
  DIrr.Collections in '..\..\DSource\DIrr.Collections.pas';

var
  i: Char;
  DriverType: E_DRIVER_TYPE;
  Device: TIrrlichtDevice;
  Driver: TVideoDriver;
  Smgr: TSceneManager;
  Mesh: TAnimatedMesh;
  Node: TSceneNode;
  lastFPS, fps: Integer;
  Str: String;
begin
  try
    { TODO -oUser -cConsole Main : Insert code here }
    ReportMemoryLeaksOnShutdown := True;

    Writeln('Please select the driver you want for this example:');
    Writeln('  (a) OpenGL 1.5');
    Writeln('  (b) Direct3D 9.0c');
    Writeln('  (c) Direct3D 8.1');
    Writeln('  (d) Burning''s Software Renderer');
    Writeln('  (e) Software Renderer');
    Writeln('  (f) OpenGL 1.5');
    Writeln('  (otherKey) exit');
    Writeln('');

    Read(i);
    i := UpCase(i);

    case i of
      'A': DriverType := EDT_OPENGL;
      'B': DriverType := EDT_DIRECT3D9;
      'C': DriverType := EDT_DIRECT3D8;
      'D': DriverType := EDT_BURNINGSVIDEO;
      'E': DriverType := EDT_SOFTWARE;
      'F': DriverType := EDT_NULL;
      else
      begin
        ExitCode := 1;
        Exit;
      end;
    end;

    Device := CreateDevice(DriverType, [640, 480]);

    if Device = nil then
    begin
      ExitCode := 1;
      Exit;
    end;

    Driver := Device.GetVideoDriver;
    Smgr := Device.GetSceneManager;

    Device.GetFileSystem.AddFileArchive('..\..\media\map-20kdm2.pk3');

    Mesh := Smgr.GetMesh('20kdm2.bsp');

    Node := nil;
    if Assigned(Mesh) then
    begin
      Node := Smgr.AddOctreeSceneNode(Mesh.GetMesh(0), nil, -1, 1024);
    end;

    if Assigned(Node) then
    begin
      Node.SetPosition(TVector3DF.Create(-1300, -144, -1249));
    end;

    smgr.AddCameraSceneNodeFPS;
    device.GetCursorControl.SetVisible(False);

    lastFPS := -1;

    while device.Run do
    begin
      if device.IsWindowActive then
      begin
        driver.BeginScene(True, True, [255, 200, 200, 200]);
        smgr.DrawAll;
        driver.EndScene;

        fps := driver.GetFPS;

        if (lastFPS <> fps) then
        begin
          Str := driver.GetName;
          Str := 'Irrlicht Engine - Quake 3 Map example [' + Str + '] FPS:' + IntToStr(fps);

          device.SetWindowCaption(PChar(Str));
          lastFPS := fps;
        end;

      end
      else
        device.Yield;
    end;

    device.Drop;

    ExitCode := 0;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
